package map.adapter;

import java.util.List;
import java.util.Vector;

import org.eclipse.emf.ecore.util.EcoreUtil;

import uimigration.Interactions.Behaviour;
import uimigration.Interactions.DisplayValue;
import uimigration.Interactions.EditContent;
import uimigration.Interactions.GraphicalRendering;
import uimigration.Interactions.InteractionsFactory;
import uimigration.Interactions.SelectContent;
import uimigration.Interactions.impl.DisplayValueImpl;
import uimigration.UIModel.Container;
import uimigration.UIModel.Event;
import uimigration.UIModel.Property;
import uimigration.UIModel.UIElement;
import uimigration.UIModel.UIModelFactory;
import uimigration.WidgetsModel.Widget;

public class UIElementFactory {
	
	public UIElementFactory( ){
	}
	
	public static void setImplementedEvent(UIElement oldElement,UIElement newElement, Widget newWt, MappingTableManager table)
	{
		if(newElement!=null)
		{
			
			Vector<String> useE= new Vector<String>();
			if(oldElement.getEvents()!=null)
			{
				newElement.getEvents().clear();
				for(Event e: oldElement.getEvents())
				{
					Event ent= getEquivalentWEvent(e.getImplementedEventBehaviour(),newWt);
					if(ent!=null)
						ent.setScriptCode(e.getScriptCode());
					else 
						ent= getEquivalentDEvent(e.getImplementedEventBehaviour(),e,table,useE);
					if(ent!=null)
						newElement.getEvents().add(ent);
				}
			}
		}
	}
	
	private static Event getEquivalentDEvent(List<Behaviour> eList,Event e, MappingTableManager table ,Vector<String> useE)
	{

		List<String> evt= table.findContactEvent(e.getName());
		if(evt.size()>0){
			for(String s : evt)
				if(!useE.contains(s))
				{
					useE.add(s);
					Event evn = UIModelFactory.eINSTANCE.createEvent();
					evn.setName(s);
					evn.setScriptCode(e.getScriptCode());
					evn.getImplementedEventBehaviour().addAll(EcoreUtil.copyAll( e.getImplementedEventBehaviour()));
					return evn;
				}
		}
		return null;
	}
	
	public static String getEquivalentEvent(List<Behaviour> eList , Widget newWt)
	{
		for(uimigration.WidgetsModel.Event e : newWt.getEvents())
		{
			if(e.getEventBehaviourLength() >0)
			{
				if(equivalentBehaviours(e.getEventBehaviourList(), eList))
					return e.getName();
			}
		}
		return "";
	}
	public static Event getEquivalentWEvent(List<Behaviour> eList , Widget newWt)
	{
		for(uimigration.WidgetsModel.Event e : newWt.getEvents())
		{
			if(e.getEventBehaviourLength() >0)
			{
				if(equivalentBehaviours(e.getEventBehaviourList(), eList))
				{
					Event ev= UIModelFactory.eINSTANCE.createEvent();
					ev.setName(e.getName());
					ev.setScriptCode(e.getHandler());
					
					ev.getImplementedEventBehaviour().addAll(e.getEventBehaviourList());
					return ev;
				}
			}
		}
		return null;
	}
	
	private static boolean equivalentBehaviours(List<Behaviour> l1, List<Behaviour> l2)
	{
		int s1= l1.size();
		for(Behaviour b1 : l1)
		{
			for(Behaviour b2: l2)
			{
				if(equalBehaviour(b1, b2))
				{
					s1--;
				}
			}
		}
		if(s1==0)
			return true;
		return false;
	}
	
	private static boolean equalBehaviour(Behaviour b1, Behaviour b2)
	{
		if(b1.eClass().getName().compareTo(b2.eClass().getName())==0)
		{
			try {
				if(((EditContent)b1).getEditatbleContent()!=null && ((EditContent)b2).getEditatbleContent()!=null)
				{
					int n =((EditContent)b1).getEditatbleContent().size();
					if(((EditContent)b1).getEditatbleContent().size()== ((EditContent)b2).getEditatbleContent().size()){
						for(int i=0;i<((EditContent)b1).getEditatbleContent().size();i++)
						{
							if(((EditContent)b1).getEditatbleContent().get(i)==(((EditContent)b1).getEditatbleContent().get(i)))
								n--;
						}
					}
					if( n==0)
						return true;
					else 
						return false;
				}
				else
					return true;
			} catch (ClassCastException e) {
				try {
					if(((SelectContent)b1).getSelectableContentList()!=null && ((SelectContent)b2).getSelectableContentList()!=null)
					{
						int n =((SelectContent)b1).getSelectableContentList().size();
						if(((SelectContent)b1).getSelectableContentList().size()== ((SelectContent)b2).getSelectableContentList().size()){
							for(int i=0;i<((SelectContent)b1).getSelectableContentList().size();i++)
							{
								if(((SelectContent)b1).getSelectableContentList().get(i)==(((SelectContent)b1).getSelectableContentList().get(i)))
									n--;
							}
						}
						if( n==0)
							return true;
						else 
							return false;
					}
					else
						return true;
				} catch (ClassCastException e2) {
				}
			}
			return true;
		}
		return false;
	}

	public static UIElement instantiateElement(UIElement oldElement, Widget newWidget, int id, MappingTableManager table){
		UIElement newElement=null;
		UIElement retElement=null;
		if(newWidget.isIsContainer())
		{	
			retElement= (Container)UIModelFactory.eINSTANCE.createContainer();
			if(oldElement.getWidgetType().isIsContainer())
				((Container)retElement).setLayout(((Container)oldElement).getLayout());
		}
		else 
			retElement= UIModelFactory.eINSTANCE.createUIElement();
		if(newWidget.getContainsWidgetLength()>0)
		{
			if(newWidget.getContainsWidget().get(0).isIsContainer())
			{	
				newElement= (Container)UIModelFactory.eINSTANCE.createContainer();
				if(oldElement.getWidgetType().isIsContainer())
					((Container)newElement).setLayout(((Container)oldElement).getLayout());
			}
			else 
				newElement= UIModelFactory.eINSTANCE.createUIElement();
			((Container)retElement).getContainsUIElement().add(newElement);
			newElement.setId(id++);
			String name=newWidget.getName();
			if(name.contains(":"))
				name=name.substring(name.indexOf(":")+1)+"1";
			name=name+oldElement.getId()+"1";
			if(oldElement.getName()!=null)
				name=oldElement.getName()+"1";
			newElement.setName(name);
			newElement.setWidgetType ( (Widget)EcoreUtil.copy(newWidget.getContainsWidget().get(0)));
			setProperties(oldElement, newElement);
			setImplementedEvent(oldElement, newElement, newWidget.getContainsWidget().get(0), table);

		}		
		retElement.setId(id);
		String name=newWidget.getName();
		if(name.contains(":"))
			name=name.substring(name.indexOf(":")+1);
		name=name+oldElement.getId();
		if(oldElement.getName()!=null)
			name=oldElement.getName();
		retElement.setName(name);
		retElement.setWidgetType ( (Widget)EcoreUtil.copy(newWidget));
		setProperties(oldElement, retElement);
		setImplementedEvent(oldElement, retElement, newWidget, table);
		return retElement;
	}


	private static void setProperties(UIElement oldE, UIElement newE)
	{
		uimigration.WidgetsModel.Property pw =getContentProperty(newE.getWidgetType());
		newE.getProperties().clear();
		int ip=newE.getProperties().size();
		int cont=0;
		if(oldE.getProperties()!=null && pw!=null)
		for(Property p : oldE.getProperties())
		{
			if (ip==2)
				break;
			if( p.getName()!=null){
				if(p.getRenderingProprety()!=null && p.getRenderingProprety().size()>0 && cont==0)
				{
					Property newPp= UIModelFactory.eINSTANCE.createProperty();
					newPp.setName(pw.getName());
					newPp.setValueType(pw.getDataType());
					if(p.getValue()!=null)
						newPp.setValue(p.getValue());
					DisplayValue gr = InteractionsFactory.eINSTANCE.createDisplayValue();
					newPp.getRenderingProprety().add(gr);
					newE.getProperties().add(EcoreUtil.copy (newPp));
					ip++;
					
					cont++;
					
				}
				else if(p.getName().compareTo("Name")==0);
				{
					Property newPp= UIModelFactory.eINSTANCE.createProperty();
					newPp.setName("Name");
					newPp.setValueType(p.getValueType());
					if(p.getValue()!=null)
						newPp.setValue(p.getValue());
					newE.getProperties().add(newPp);
					ip++;
				}
			}
			
		}
	}
	private static uimigration.WidgetsModel.Property getContentProperty(Widget tw){
		if(tw!=null)
		for(uimigration.WidgetsModel.Property pj: tw.getProperties())
		{
			if(pj.getRenderingProperty()!=null)
				return EcoreUtil.copy( pj);
		}
		return null;

	}
	/**
	 * 
	 * @param container Container widget have the contains widget in target plate-forme
	 * @param newWidget new contained widget
	 * @return new Contains widget
	 */
	public static Container instantiateElementSame(UIElement oldElement,Container container, Widget widgetType,MappingTableManager table) {
		Container newElement=null;
		{
			if(container.getContainsUIElement().size()==1&&((Container)container.getContainsUIElement().get(0)).getContainsUIElement().size()==0 )
				newElement=(Container) container.getContainsUIElement().get(0);
			else
			{	
				newElement= (Container)UIModelFactory.eINSTANCE.createContainer();
			}
			newElement.setWidgetType( (Widget)EcoreUtil.copy(widgetType));
			newElement.setLayout(container.getLayout());
			int id=Integer.parseInt(container.getId()+""+container.getContainsUIElement().size());
			newElement.setId(id);
			String name=widgetType.getName();
			if(name.contains(":"))
				name=name.substring(name.indexOf(":")+1);
			name=name+newElement.getId();
			if(container.getName()!=null)
				name= container.getName()+"1";
			newElement.setName(name);
			setProperties(oldElement, newElement);
			setImplementedEvent(container, newElement, widgetType,  table);
		}
		return newElement;
	}
}
